package srms.the504div1;

public class MathContest_Best {
	public int countBlack(String ballSequence, int repetitions) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < repetitions; i++) {
			sb.append(ballSequence);
		}
		String balls = sb.toString();
		boolean inverted = false;
		int position = 0, direction = 1;
		int low = 0, high = balls.length() - 1;
		int shownNumber = 0;

		for (int i = 0; i < balls.length(); i++) {
			if (direction == 1) {
				low++;
			} else {
				high--;
			}

			if (!inverted) {
				if (balls.charAt(position) == 'B') {
					shownNumber++;
					inverted = true;
					position += direction;
				} else {
					if (direction == 1) {
						position = high;
					} else {
						position = low;
					}
					direction *= -1;
				}
			} else {
				if (balls.charAt(position) == 'W') {
					shownNumber++;
					inverted = false;
					position += direction;
				} else {
					if (direction == 1) {
						position = high;
					} else {
						position = low;
					}
					direction *= -1;
				}
			}
		}

		return shownNumber;
	}
}
